/**
 * 用于处理主页相关的业务
 */
var eventproxy = require('eventproxy');
var cateModel = require('../models/cateModel');
var topicModel = require('../models/topicModel');
var userModel = require('../models/userModel');

var index = {};

//跳转到主页
index.goIndex = function(req,res){
    //console.log("==session's username is: "+req.session.username);
	//res.render('index', {username: req.session.username,userid:req.session.userid,tempName:'index'});
	res.render('index',{tempName:"index"});
};

//跳转到登陆页
index.goLogin = function (req,res) {
    //console.log(req.session);
	if(req.session.username){
		res.redirect("/");
	}else{
		res.render('login',{tempName:'login'});
	}
};

index.goRegister = function (req,res) {
    res.render('register',{tempName:'register'});
};

//进入论坛所进行的处理 (跳页)
index.goForum = function (req,res) {
	//console.log(req.query.cate);
	var topicCate = "";
	var con = {};

	//分页参数
	var currentPage = 1;	//跳转目标页数
	
	if(req.query.page == undefined){
		currentPage	 = 1;
	}else{
		currentPage = req.query.page;
	}
	//console.log(currentPage);

	//判断话题分类
	if(req.query.cate !== undefined && req.query.cate !== "" && req.query.cate !== "all"){
		if(req.query.cate === "elite"){
			con = {
				isElite: true
			};
			topicCate = "elite";
		}else{
			con = {
				topicType: req.query.cate
			};
			topicCate = req.query.cate;
		}
	}else{
		topicCate = "all";
	}

	var ep = new eventproxy();
	ep.all("cateData","topicData","noReplyTopics","score",function (cateData,topicData,noReplyTopics,score) {
		//console.log(cateData);
		//console.log(topicList);
		//console.log(noReplyTopics);
		//console.log(score);
		var pageData = {
			tempName: 'forum',
			topicCate: topicCate,
			cateData: cateData,
			topicList: topicData.topicsList,
			topicsAmount: topicData.topicsAmount,
			pagesAmount: topicData.pagesAmount,
			currentPage: currentPage,
			noReplyTopics: noReplyTopics,
			score: score
		};
		//console.log(pageData);
		res.render('forum',pageData);

		/**
		 * var datas = {
				topicsList: data,
				currentPage: currentPage,
				topicsAmount: amount,
				pagesAmount: pagesAmount
			};
		 */
	});
	
	//获取所有话题分类列表
	cateModel.find({},function (err,data) {
		ep.emit("cateData",data);
	});

	//获取所有话题列表(根据是否置顶及时间先后顺序排序)
	/* topicModel.find(con,{},{sort:{isTop:-1,createTime:-1}})
	.populate('topicType',{catename:1})
	.populate("poster",{avatar:1,username:1})
	.skip(currentPage)	//表示跳到第几页
	.limit(pageLimit)	//每页包含数据数量
	.exec(function (err,data) {
		topicModel.find(con).count(function(err,amount){
			var topicsLeft = amount % pageLimit;	//余数 如果大于0
			var	pagesAmount = parseInt(amount / pageLimit);		//获取最大页数
			if(topicsLeft > 0){//如果余数大于0, 说明有被每页显示值整除不了的数量, 因此要额外加一页显示
				pagesAmount += 1;	
			}
			var datas = {
				topicsList: data,
				currentPage: currentPage,
				topicsAmount: amount,
				pagesAmount: pagesAmount
			};
			console.log("--page amount: "+pagesAmount);
			console.log("---current page: "+currentPage);
			console.log(data);
			ep.emit("topicData",datas);
		});

		
	}); */

	//获取所有话题列表(根据是否置顶及时间先后顺序排序)
	var pageSize = 14;	//页面话题数
	var pageOffset = (currentPage-1)*pageSize;	//偏移量
	topicModel.find(con,{},{
		sort: {isTop:-1,createTime:-1},
		skip: pageOffset,
		limit: pageSize
	})
	.populate('topicType',{catename:1})
	.populate("poster",{avatar:1,username:1})
	.exec(function (err,data) {
		topicModel.find(con).count(function(err,amount){
			var pagesLeft = amount % pageSize;	//能被页面话题数整除所余留的多余话题数
			var pagesAmount = parseInt(amount / pageSize);	//所有话题页页数
			if(pagesLeft > 0){
				pagesAmount += 1;	
			}
			var datas = {
				topicsList: data,
				topicsAmount: amount,
				pagesAmount: pagesAmount
			};
			ep.emit("topicData",datas);
		});
	}); 

	//获取5条回复量为0的帖子, 并按照时间从晚到早的顺序排列
	topicModel.find({
		reply:{$size:0}
	},{
		title:1
	},{
		sort:{createTime:-1},
		limit: 5
	},function (err,data) {
		ep.emit("noReplyTopics",data);
	});

	//获取积分榜前5的用户
	userModel.find({},{username:1,score:1},{sort:{score:-1},limit:5},function (err,data) {
		ep.emit("score",data);
	});
}

module.exports = index;